package com.sgq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgq.entity.User;
import com.sgq.dao.UserMapper;
import com.sgq.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.vo.UserQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sgq
 * @since 2024-09-13
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public IPage<User> findPage(UserQueryVo userQueryVo) {
        //传递一个page对象 =》 当前页  数据条数大小
        System.out.println("userQueryVo = " + userQueryVo);
        Long pageNum = userQueryVo.getPageNum();
        Long pageSize = userQueryVo.getPageSize();
        IPage<User> page = new Page<>(pageNum,pageSize);
        //条件按名字
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //判断条件是否为空 "username"
        queryWrapper.like(!ObjectUtils.isEmpty(userQueryVo.getUsername()), User::getUsername, userQueryVo.getUsername());

        IPage<User> iPage = this.baseMapper.selectPage(page, queryWrapper);
        return iPage;
    }

    @Override
    public User findUserByUserName(String username) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        //                "username"
        queryWrapper.eq(User::getUsername,username);
        return this.baseMapper.selectOne(queryWrapper);
    }
}
